// pages/shopcar/shopcar.js
import { shopcarData, removeShopcar, updateShopcar }  from '../../api/goods'
let app = getApp();
Page({
  data: {
    goods: [],
    totalPrice: 0,
    isAllSelected: 0,
    mode: true,
    isSelectedDelete: 0,
    isDisabled: false,
    type: 'is_selected',
    AllSelectedType: 'isAllSelected',
    isLogin:app.globalData.verifyLogin,//是否登录
  },
  onLoad: function (options) {

  },

  // 点击登录
  async clickLogin() {
    let {errCode} = await app.login();
    if(errCode == 0) {
      this.setData({
        isLogin:true
      })
      this.getShopcarData();
    }
  },

  // 跳转商品详情
  goodsInfoTo(e) {
    let {goods} = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/goodsInfo/goodsInfo?goodsId=${goods.goods_id_alias}`,
    })
  },

  async getShopcarData() {
    if(!this.data.isLogin){return;} //没登录不进入下一步
    const {data} = await shopcarData()
    let isAllSelected = 1
    let isDisabled 
    data.forEach(item => {
        if (item.is_selected == false) isAllSelected = 0
        if (item.is_selected == true) isDisabled = false
    })
    this.setData({
      goods: data,
      isAllSelected,
      isDisabled
    })
    this.changeTotalPrice()
  },
  // 子checkbox 改变的监听
  checkboxStatus(e) {
    const goodsLength = this.data.goods.length
    const selectedLength = e.detail.value.length
    // if (this.data.mode) {
    //   let isDisabled = true
    //   this.data.goods.forEach(item => {
    //     item.goods_id = item.goods_id + ''
    //     if (e.detail.value.includes(item.goods_id)){
    //       item.is_selected = 1
    //       isDisabled = false
    //     } else {
    //       item.is_selected = 0
    //     }
    //   })
    //   if (goodsLength === selectedLength) {
    //     this.setData({
    //       isAllSelected: 1,
    //       goods: this.data.goods,
    //       isDisabled
    //     })
    //   } else{
    //     this.setData({
    //       isAllSelected: 0,
    //       goods: this.data.goods,
    //       isDisabled
    //     })
    //   }
    //   this.changeTotalPrice()
    // } else {
    //   let isDisabled = true
    //   this.data.goods.forEach(item => {
    //     item.goods_id = item.goods_id + ''
    //     if (e.detail.value.includes(item.goods_id)){
    //       item.delete_selected = 1
    //       isDisabled = false
    //     } else {
    //       item.delete_selected = 0
    //     }
    //   })
    //   if (goodsLength === selectedLength) {
    //     this.setData({
    //       isSelectedDelete: 1,
    //       isDisabled: isDisabled
    //     })
    //   } else{
    //     this.setData({
    //       isSelectedDelete: 0,
    //       isDisabled: isDisabled
    //     })
    //   }
    // }
    const type = this.data.type
    const AllSelectedType = this.data.AllSelectedType
    let isDisabled = true
    this.data.goods.forEach(item => {
      item.goods_id = item.goods_id + ''
      if (e.detail.value.includes(item.goods_id)){
        item[type] = 1
        isDisabled = false
      } else {
        item[type] = 0
      }
    })
    if (goodsLength === selectedLength) {
      this.setData({
        [AllSelectedType]: 1,
        goods: this.data.goods,
        isDisabled
      })
    } else{
      this.setData({
        [AllSelectedType]: 0,
        goods: this.data.goods,
        isDisabled
      })
    }
    this.data.mode && this.changeTotalPrice()
  },
  // 点击全选checkbox
  allSelectedBtn() {
    
    // if (this.data.mode) {
    //   this.setData({
    //     isAllSelected: !this.data.isAllSelected
    //   })
    //   if (this.data.isAllSelected) {
    //     this.data.goods.forEach(item => {
    //       item.is_selected = 1
    //     })
    //   } else {
    //     this.data.goods.forEach(item => {
    //       item.is_selected = 0
    //     })
    //   }
    //   this.setData({
    //     goods: this.data.goods
    //   })
    //   this.changeTotalPrice()
    // } else {
    //   this.setData({
    //     isSelectedDelete: !this.data.isSelectedDelete
    //   })
    //   this.data.goods.forEach(item => {
    //     item.delete_selected = this.data.isSelectedDelete
    //   })
    //   this.setData({
    //     goods: this.data.goods,
    //     isDisabled: !this.data.isSelectedDelete
    //   })
    // }
    const type = this.data.type
    const AllSelectedType = this.data.AllSelectedType
    this.setData({
      [AllSelectedType]: !this.data[AllSelectedType]
    })
    this.data.goods.forEach(item => {
      item[type] = this.data[AllSelectedType]
    })
    this.setData({
      goods: this.data.goods,
      isDisabled: !this.data[AllSelectedType]
    })
    this.data.mode && this.changeTotalPrice()
  },
  // 商品数量的改变
  numChange(e) {
    const num = e.detail
    const goodsId = e.currentTarget.dataset.goods_id
    this.data.goods.find(item => {
      if (item.goods_id === goodsId) item.goods_num = num
    })
    this.setData({
      goods: this.data.goods
    })
    this.changeTotalPrice()
  },
  // 改变总价格
  changeTotalPrice() {
    let totalPrice = 0
    this.data.goods.forEach(item => {
      item.is_selected && (totalPrice = totalPrice + item.goods_num * item.real_price)
    })
    this.setData({
      totalPrice: totalPrice * 100
    })
  },
  // 移除购物车商品
  removeShopcar (e) {
    const {goodsid} = e.currentTarget.dataset
    const index = this.data.goods.findIndex(item => {
      return item.goods_id === goodsid
    })
    this.data.goods.splice(index,1)
    removeShopcar([goodsid])
    this.setData({
      goods: this.data.goods
    })
  },
  // 去首页
  toHomePage () {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },
  // 切换编辑模式
  toggleMode () {
    this.setData({
      mode: !this.data.mode
    })
    if (!this.data.mode) {
      this.data.goods.forEach(item => {
        item.delete_selected = 0
      })
      this.setData({
        goods: this.data.goods,
        isSelectedDelete: 0
      })
    }
    const type = this.data.mode ? 'is_selected' : 'delete_selected'
    const AllSelectedType = this.data.mode ? 'isAllSelected' : 'isSelectedDelete'
    const isDisabled = !this.data.goods.some(item => {
      return item[type] == true
    })
    this.setData({
      isDisabled,
      type,
      AllSelectedType
    })
  },
  // 触发结算 或 删除按钮
  commitBtn() {
    this.data.mode ? this.commitOrder() : this.deleteBtn()
  },
  // 触发删除按钮
  deleteBtn() {
    const flag = this.data.goods.filter(item => {
      return item.delete_selected == false
    })
    const totalLength = this.data.goods.length
    const deleteLength = flag.length
    wx.showModal({
      content: `确认删除所选店铺的${totalLength - deleteLength}个商品？`,
      success: (res) => {
        if (res.confirm) {
          let goodsIds = []
          this.data.goods.forEach(item => {
            item.delete_selected && goodsIds.push(item.goods_id)
          })
          this.setData({
            goods: flag
          })
          removeShopcar(goodsIds)
        }
        if (res.cancel) {
        }
      }
    })
  },
  // 触发提交订单 
  commitOrder() {
    let goodsId = []
    this.data.goods.forEach(item => {
      if (item.is_selected) {
        goodsId.push({
          alias: item.goods_id_alias,
          num: item.goods_num
        })
      }
    })
    wx.navigateTo({
      url: '/pages/orderCommit/order?goodsId=' + JSON.stringify(goodsId)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({ //登录状态
      isLogin:app.globalData.verifyLogin
    })
    // 用openid 获取用户的购物车数据
    this.getShopcarData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    updateShopcar(this.data.goods)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  }
})